package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.R

/**
 * Created by DomenCai on 2022/4/1.
 */
class DashAdapter : RecyclerView.Adapter<DashAdapter.DashHolder>() {

    private val dashList: MutableList<Dash> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: ArrayList<Dash>) {
        dashList.clear()
        dashList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class DashHolder(view: View) : RecyclerView.ViewHolder(view) {
        val dashHead: ImageView = view.findViewById(R.id.dash_head)
        val dashNameTV: TextView = view.findViewById(R.id.dashName_TV)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DashHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_dash, parent, false)
        return DashHolder(view)
    }

    override fun onBindViewHolder(holder: DashHolder, position: Int) {
        val dash = dashList[position]
        holder.dashHead.setImageResource(dash.dashHead)
        holder.dashNameTV.text = dash.dashName
    }

    override fun getItemCount(): Int = dashList.size
}